package com.umeox.recorder

import android.content.Intent
import android.os.Bundle
import android.view.ViewGroup
import androidx.core.content.ContextCompat.startActivity
import androidx.recyclerview.widget.RecyclerView.Adapter
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.umeox.moto.common.utils.GsonUtils
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity
import com.umeox.recorder.data.RecorderBean
import com.umeox.recorder.data.RecorderDataBase
import com.umeox.recorder.databinding.ActivityRecordListBinding
import com.umeox.recorder.databinding.ItemRecordBinding
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

class RecordListActivity : ThemeAppCompatActivity() {

    private lateinit var mBinding: ActivityRecordListBinding
    private lateinit var mDeleteDialog: RecordDeleteDialog
    private val mRecorders = mutableListOf<RecorderBean>()
    private var mSubscribe: Disposable? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initView()
        initData()
    }

    private fun initView() {
        mBinding = ActivityRecordListBinding.inflate(layoutInflater)
        setContentView(mBinding.root)
        mDeleteDialog = RecordDeleteDialog(this)
        mBinding.btnAdd.setOnClickListener { addRecord() }
        mBinding.rvRecorder.adapter = RecordAdapter()
    }

    private fun initData() {
        getRecords()
    }

    private fun getRecords() {
        val dao = RecorderDataBase.getInstance(this).recorderDao
        mSubscribe = dao
            .query().subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                mRecorders.clear()
                mRecorders.addAll(it)
                //noinspection NotifyDataSetChanged
                mBinding.rvRecorder.adapter?.notifyDataSetChanged()
                mBinding.viewEmpty.visibility = if (it.isNullOrEmpty()) ViewGroup.VISIBLE else ViewGroup.GONE
            }, {
                it.printStackTrace()
            })
    }


    private fun addRecord() {
        val intent = Intent(this, RecordAddActivity::class.java)
        startActivity(intent)
    }

    private fun deleteRecord(record: RecorderBean) {
        mDeleteDialog.show(record)
    }

    private fun playRecord(record: RecorderBean) {
        val data = GsonUtils.toJson(record)
        val intent = Intent(this, RecordPlayActivity::class.java)
        intent.putExtra("data", data)
        startActivity(intent)
    }

    private inner class RecordAdapter : Adapter<ViewHolder>() {
        val mDateFormat = SimpleDateFormat("yyyy/MM/dd HH:mm", Locale.getDefault())
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            val binding = ItemRecordBinding.inflate(layoutInflater, parent, false)
            val holder = object : ViewHolder(binding.root) {}
            binding.root.setOnClickListener {
                val item = mRecorders[holder.adapterPosition]
                playRecord(item)
            }
            binding.root.setOnLongClickListener {
                val item = mRecorders[holder.adapterPosition]
                deleteRecord(item)
                true
            }
            return holder
        }

        override fun getItemCount(): Int {
            return mRecorders.size
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            val binding = ItemRecordBinding.bind(holder.itemView)
            val bean = mRecorders[position]
            val name = "\u200E${bean.name}"
            binding.tvName.text = name
            binding.tvDate.text = mDateFormat.format(Date(bean.createTime))
        }
    }
}
